package com.hjj.controller;

import com.hjj.beans.User;
import com.hjj.dto.ResultDTO;
import com.hjj.exception.LoginException;
import com.hjj.services.UserServices;
import org.apache.commons.codec.digest.DigestUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.UUID;


@RestController
@RequestMapping("/login")
public class LoginController {
    @Autowired
    @Qualifier("userServicesImp")
    UserServices userServices;

    @Autowired
    @Qualifier("userServicesFilter")
    UserServices userServicesFilter;

    @RequestMapping("in.action")
    public ResultDTO in(String loginAct, String loginPwd, String rememberMe, HttpSession httpSession, HttpServletResponse response) throws LoginException {

        User user = userServices.getByUser(loginAct, loginPwd);

        if (user == null) throw new LoginException("用户名或密码错误");

        // 登录成功将用户数据添加到session中
        httpSession.setAttribute("user", user);

        // cookie 创建三部曲
        // 1——设置cookie的键值对【键和值必须蓄能是String】
        // 2——设置cookie的键值对作用域，也就是那个地址可以用【这里设置根地址，意味着任何地方都可以调用cookie里面的值】
        // 3——设置cookie的年龄

        // 设置cookie的键值对【键和值必须蓄能是String】
        Cookie loginActCookie = new Cookie("loginAct", loginAct);
        Cookie loginPwdCookie = new Cookie("loginPwd", DigestUtils.md5Hex(loginPwd));

        // 设置cookie的键值对作用域，也就是那个地址可以用【这里设置根地址，意味着任何地方都可以调用cookie里面的值】
        loginActCookie.setPath("/");
        loginPwdCookie.setPath("/");

        // 判断是否需要十天内免登陆
        if ("true".equals(rememberMe)) {
            // 设置cookie的年龄
            loginActCookie.setMaxAge(60 * 60 * 24 * 10);
            loginPwdCookie.setMaxAge(60 * 60 * 24 * 10);

        }else {
            loginActCookie.setMaxAge(0);
            loginPwdCookie.setMaxAge(0);
        }


        // 响应的时候告诉浏览器添加cookie
        response.addCookie(loginActCookie);
        response.addCookie(loginPwdCookie);

        ResultDTO resultDTO = new ResultDTO();
        resultDTO.setResult(ResultDTO.SUCCESS);
        return resultDTO;
    }

    @RequestMapping("userInfo.action")
    public ResultDTO userInfo(HttpSession httpSession) throws LoginException {
        User user = (User) httpSession.getAttribute("user");

        if (user == null) throw new LoginException("还没有登录");

        ResultDTO resultDTO = new ResultDTO();
        resultDTO.setResult(ResultDTO.SUCCESS);
        resultDTO.setData(user);

        return resultDTO;
    }


    @RequestMapping("out.action")
    public ResultDTO out(HttpServletResponse response, HttpSession httpSession){

        // 删除session中的登录者数据
        httpSession.removeAttribute("user");


        // 创建cookie对象
        Cookie loginActCookie =  new Cookie("loginAct", "");
        Cookie loginPwdCookie =  new Cookie("loginPwd", "");

        // 设置cookie的作用域
        loginActCookie.setPath("/");
        loginPwdCookie.setPath("/");

        // 设置cookie的年龄
        loginActCookie.setMaxAge(0);
        loginPwdCookie.setMaxAge(0);

        // 替换掉cookies中的对象
        response.addCookie(loginActCookie);
        response.addCookie(loginPwdCookie);


        ResultDTO resultDTO = new ResultDTO();
        resultDTO.setResult(ResultDTO.SUCCESS);

        return resultDTO;
    }


    @RequestMapping("automaticWriting.action")
    public ResultDTO automaticWriting(HttpServletRequest request) throws LoginException {

        Cookie[] cookies = request.getCookies();

        if (cookies == null){
            throw new LoginException("你还从来没有登录过");
        }

        HashMap<String, String> hashMap = new HashMap<>();

        // 获取cookies中存储的用户数据
        for (Cookie cookie : cookies) {
            if ("loginAct".equals(cookie.getName())) {
                hashMap.put("loginAct", cookie.getValue());
            } else if ("loginPwd".equals(cookie.getName())) {
                hashMap.put("loginPwd", cookie.getValue());
            }
        }

        ResultDTO resultDTO = new ResultDTO();
        resultDTO.setResult(ResultDTO.SUCCESS);
        resultDTO.setData(hashMap);

        return resultDTO;
    }

    @RequestMapping("automaticLogin.action")
    public ResultDTO automaticLogin(String loginAct, String loginPwd, String rememberMe) throws LoginException {

        User user = userServicesFilter.getByUser(loginAct, loginPwd);

        if (user == null) throw new LoginException("账户或密码错误");



        ResultDTO resultDTO = new ResultDTO();
        resultDTO.setResult(ResultDTO.SUCCESS);
        return resultDTO;
    }

}
